package com.canbot.u05.activity.presidemode.newcode.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IItemCheck;

public class ItemGridView extends View {
    private static final String TAG = "ItemGridView";
    private Bitmap mBitmapBk;
    private Bitmap mBitmapCheck;
    private int padding;
    private String context = "001";
    private TextPaint textPaint;
    private float textW;
    private boolean checked = false;
    private IItemCheck iItemCheck;
    private boolean openText;

    public ItemGridView(Context context) {
        this(context, null);
    }

    public ItemGridView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public ItemGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttribute(context, attrs);
        initData();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(mBitmapBk.getWidth() + mBitmapCheck.getWidth() / 2 - padding * 2,
                mBitmapBk.getHeight() + mBitmapCheck.getHeight() / 2 - padding * 2);
    }

    private void initData() {
        mBitmapCheck = BitmapFactory.decodeResource(getResources(), R.drawable.nitem_bg_select);
        padding = (int) getResources().getDimension(R.dimen.x6);
        textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        textPaint.setColor(getResources().getColor(R.color.item_music_text));
        textPaint.setTextSize((int) getResources().getDimension(R.dimen.common_back_text_size));
        textW = textPaint.measureText(context);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                checked = !checked;
                if (iItemCheck != null) {
                    iItemCheck.onChange(checked);
                }
            }
        });
    }


    private void initAttribute(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ItemGridView);
        openText = typedArray.getBoolean(R.styleable.ItemGridView_openText, false);
        BitmapDrawable drawable = (BitmapDrawable) typedArray.getDrawable(R.styleable.ItemGridView_backdrawable);
        mBitmapBk = drawable.getBitmap();
        typedArray.recycle();
    }

    public void cancleCheck() {
        checked = false;
        invalidate();
    }

    public void check() {
        checked = true;
        invalidate();
    }

    public void setContext(String context) {
        this.context = context;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(mBitmapBk, 0, mBitmapCheck.getHeight() / 2 - padding * 2, null);
        if (checked) {
            canvas.drawBitmap(mBitmapCheck, getWidth() - mBitmapCheck.getWidth(), 0, null);
        }
        if (openText) {
            canvas.drawText(context, mBitmapBk.getWidth() / 2 - textW / 2, mBitmapBk.getHeight(), textPaint);
        }
    }

    public void setiItemCheck(IItemCheck iItemCheck) {
        this.iItemCheck = iItemCheck;
    }
}
